package com.kb.erp.entity.quote;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

@Data
public class GoodsData {
    @JsonProperty("goodsSn")
    private String goodsSn;

    @JsonProperty("modelId")
    private int modelId;

    @JsonProperty("modelName")
    private String modelName;

    @JsonProperty("brandId")
    private int brandId;

    @JsonProperty("brandName")
    private String brandName;

    @JsonProperty("encapsulationValue")
    private String encapsulationValue;

    @JsonProperty("stockNum")
    private int stockNum;

    @JsonProperty("hkStockNum")
    private int hkStockNum;

    @JsonProperty("szStockNum")
    private int szStockNum;

    @JsonProperty("mpq")
    private int mpq;

    @JsonProperty("muq")
    private int muq;

    @JsonProperty("moq")
    private int moq;

    @JsonProperty("type")
    private int type;

    @JsonProperty("description")
    private String description;

    @JsonProperty("weight")
    private String weight;

    @JsonProperty("dateCode")
    private String dateCode;

    @JsonProperty("needSplitGoods")
    private int needSplitGoods;

    @JsonProperty("priceStagesRMBTax")
    private List<PriceStage> priceStagesRMBTax;

    @JsonProperty("priceStagesCNUSD")
    private List<PriceStage> priceStagesCNUSD;

    @JsonProperty("priceStagesHkUSD")
    private List<PriceStage> priceStagesHkUSD;

    @Data
    public static class PriceStage {
        @JsonProperty("ladderNumber")
        private int ladderNumber;

        @JsonProperty("ladderTaxPrice")
        private double ladderTaxPrice;

        @JsonProperty("ladderTaxPriceAmount")
        private double ladderTaxPriceAmount;


    }

    public String generateNumString(String currency) {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;

        if(Objects.equals(currency, "USD")){
            // Handling MyPricing
            if (priceStagesCNUSD != null && !priceStagesCNUSD.isEmpty()) {
                for (PriceStage myPrice : priceStagesCNUSD) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb .append(myPrice.getLadderNumber());


                    isFirst = false;
                }
            }

            return sb.toString();
        }else if(Objects.equals(currency,"RMB")){

            if (priceStagesRMBTax != null && !priceStagesRMBTax.isEmpty()) {
                for (PriceStage myPrice : priceStagesRMBTax) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb .append(myPrice.getLadderNumber());


                    isFirst = false;
                }
            }

            return sb.toString();

        }

        return null;

    }

    public String generatePriceString(String currency) {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        if(Objects.equals(currency, "USD")){
            // Handling MyPricing
            if (priceStagesCNUSD != null && !priceStagesCNUSD.isEmpty()) {
                for (PriceStage myPrice : priceStagesCNUSD) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb .append(myPrice.getLadderTaxPrice());


                    isFirst = false;
                }
            }
            return sb.toString();
        }else if(Objects.equals(currency,"RMB")){
            if (priceStagesRMBTax != null && !priceStagesRMBTax.isEmpty()) {
                for (PriceStage myPrice : priceStagesRMBTax) {
                    if (!isFirst) {
                        sb.append("<br/>");
                    }
                    sb .append(myPrice.getLadderTaxPrice());
                    isFirst = false;
                }
            }
            return sb.toString();
        }
        return null;

    }

    public void findMaxLadderNumberLessThanOrEqual(int number,String currency,  QuoteVO tem) {
        if(Objects.equals(currency, "USD")){
            PriceStage priceStage1 = priceStagesCNUSD.stream()
                    .filter(priceStage -> priceStage.getLadderNumber() <= number)
                    .max((ps1, ps2) -> Integer.compare(ps1.getLadderNumber(), ps2.getLadderNumber()))
                    .orElse(null);

            if(priceStage1!=null){
                tem.setResult(String.valueOf(priceStage1.getLadderNumber()));
                tem.setResult1(String.valueOf(priceStage1.getLadderTaxPrice()));
                tem.setResult2(String.valueOf(priceStage1.getLadderTaxPrice()));
                tem.setResult4(String.valueOf(new BigDecimal(BigInteger.ONE)));
            }
        }else if(Objects.equals(currency,"RMB")){
            PriceStage priceStage1 = priceStagesRMBTax.stream()
                    .filter(priceStage -> priceStage.getLadderNumber() <= number)
                    .max((ps1, ps2) -> Integer.compare(ps1.getLadderNumber(), ps2.getLadderNumber()))
                    .orElse(null);

            if(priceStage1!=null){
                tem.setResult(String.valueOf(priceStage1.getLadderNumber()));
                tem.setResult1(String.valueOf(priceStage1.getLadderTaxPrice()));
                tem.setResult2(String.valueOf(priceStage1.getLadderTaxPrice()));
                tem.setResult4(String.valueOf(new BigDecimal(BigInteger.ONE)));
            }
        }




    }



}
